<?php
/**
 * Created by PhpStorm.
 * User: czw
 * Date: 2018/7/11
 * Time: 14:59
 */
namespace app\common\model;

use app\lib\exception\BaseException;

class PieceShare extends BaseModel
{
    public function piece(){
        return $this->hasOne('product_piece','id','piece_id');
    }
    public static function updateOne($share_id,$data){
        $res = (new self())->allowField(true)->save($data,['id'=>$share_id]);
        if(!$res){
            throw new BaseException(['msg'=>'没有改变！']);
        }
    }
    public static function getById($id){
        $res = (new self())->with('piece')->find($id);
        if(!$res) throw new BaseException(['msg'=>'没有分享记录']);
        return $res;
    }
    public static function getByUserId($user_id,$field='*'){
        return self::where('user_id='.$user_id)->with(['piece'])->
        field($field)->order('receive_time DESC')->select();
    }
    public function getAll($where){
        return self::where($where)->select();

    }
}